package com.jiahao.material.controllers;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiahao.material.entity.*;
import com.jiahao.material.mapper.WarehouseMaterialMapper;
import com.jiahao.material.services.IRoleService;
import com.jiahao.material.services.IWarehouseingService;
import com.jiahao.material.utils.ResultUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/warehousing")
public class WarehouseingController {

    @Autowired
    private IWarehouseingService warehouseingService;

    @Autowired
    private WarehouseMaterialMapper warehouseMaterialMapper;

    @RequiresPermissions("addWarehousing")
    @PostMapping("/add")
    public Result add(@RequestBody Warehouseing warehouseing) throws Exception{
        warehouseing.setStatus("02");
        warehouseingService.save(warehouseing);
        return ResultUtil.success("添加入库成功!");
    }


    @PostMapping("/page")
    public Result getByPage(@RequestBody Map<String,Object> map) throws Exception{
        int pageSize = map.get("pageSize") == null ? 10 : Integer.parseInt(map.get("pageSize").toString());
        int currentPage = map.get("currentPage") == null ? 1 : Integer.parseInt(map.get("currentPage").toString());
        Page<Warehouseing> page = new Page<>(currentPage,pageSize);

        QueryWrapper<Warehouseing> warehouseingQueryWrapper = new QueryWrapper<>();

        //根据需求添加查询条件
        if (map.get("warehouseId")!=null && !map.get("warehouseId").toString().equals("")){
            warehouseingQueryWrapper.eq("warehouseId",map.get("warehouseId").toString());
        }

        if (map.get("wcode")!=null && !map.get("wcode").toString().equals("")){
            warehouseingQueryWrapper.like("wcode",map.get("wcode").toString());
        }

        if (map.get("supplierId")!=null && !map.get("supplierId").toString().equals("")){
            warehouseingQueryWrapper.eq("supplierId",map.get("supplierId").toString());
        }

        if (map.get("materialId")!=null && !map.get("materialId").toString().equals("")){
            warehouseingQueryWrapper.eq("materialId",map.get("materialId").toString());
        }
        if (map.get("status")!=null && !map.get("status").toString().equals("")){
            warehouseingQueryWrapper.eq("status",map.get("status").toString());
        }


        IPage<Warehouseing> page1 = warehouseingService.page(page, warehouseingQueryWrapper);
        return ResultUtil.success("",page1);
    }

    @GetMapping("/byId/{id}")
    public Result getById(@PathVariable String id) throws Exception{
        Warehouseing byId = warehouseingService.getById(id);
        return ResultUtil.success("",byId);
    }

    @RequiresPermissions("updateWarehousing")
    @PutMapping("/update")
    public Result update(@RequestBody Warehouseing warehouseing) throws Exception{
        if (warehouseing.getStatus().equals("01")){
            warehouseing.setSupplierStatus("02");
        }
        warehouseingService.updateById(warehouseing);
        return ResultUtil.success("更新入库成功!");
    }

    @RequiresPermissions("agree")
    @PostMapping("/agree")
    @Transactional
    public Result agree(@RequestBody Warehouseing warehouseing) throws Exception{
        String wid = warehouseing.getWarehouseId();
        String mid = warehouseing.getMaterialId();

        //查询条件
        QueryWrapper<WarehouseMaterial> warehouseMaterialQueryWrapper = new QueryWrapper<>();
        warehouseMaterialQueryWrapper.eq("wid",wid);
        warehouseMaterialQueryWrapper.eq("mid",mid);
        WarehouseMaterial warehouseMaterial = warehouseMaterialMapper.selectOne(warehouseMaterialQueryWrapper);

        //更新条件
        UpdateWrapper<WarehouseMaterial> warehouseMaterialUpdateWrapper = new UpdateWrapper<>();
        warehouseMaterialUpdateWrapper.eq("wid",wid);
        warehouseMaterialUpdateWrapper.eq("mid",mid);
        if (warehouseMaterial!=null){
            warehouseMaterial.setCount(warehouseMaterial.getCount()+warehouseing.getCount());
            warehouseMaterialMapper.update(warehouseMaterial,warehouseMaterialUpdateWrapper);
        }else{
            warehouseMaterial = new WarehouseMaterial();
            warehouseMaterial.setMid(mid);
            warehouseMaterial.setWid(wid);
            warehouseMaterial.setCount(warehouseing.getCount());
            warehouseMaterialMapper.insert(warehouseMaterial);
        }
        //设置状态
        warehouseing.setSupplierStatus("01");
        warehouseingService.updateById(warehouseing);

        return ResultUtil.success("已出货入库");


    }

    @RequiresPermissions("refuse")
    @PostMapping("/refuse")
    @Transactional
    public Result refuse(@RequestBody Warehouseing warehouseing) throws Exception{
        warehouseing.setSupplierStatus("02");
        warehouseingService.updateById(warehouseing);
        return ResultUtil.success("已拒绝出货入库");
    }

    @RequiresPermissions("delWarehousing")
    @DeleteMapping("/del/{id}")
    public Result delById(@PathVariable String id) throws Exception{
        warehouseingService.removeById(id);
        return ResultUtil.success("删除入库成功!");
    }

    @RequiresPermissions("delWarehousing")
    @PostMapping("/dels")
    public Result delByIds(@RequestBody List<String> ids) throws Exception{
        warehouseingService.removeByIds(ids);
        return ResultUtil.success("删除入库成功!");
    }

}
